{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NPU 使用案例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型对话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "requests.get(url=\"http://localhost:8020/v1/models\").json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from IPython import display\n",
    "\n",
    "base_url = \"http://localhost:8020/v1/\"\n",
    "client = OpenAI(api_key=\"EMPTY\", base_url=base_url)\n",
    "\n",
    "messages = [{\n",
    "                \"role\": \"user\",\n",
    "                \"content\": \"请介绍一下你自己\"\n",
    "            }]\n",
    "use_stream=False\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "        model=\"glm-4\",\n",
    "        messages=messages,\n",
    "        stream=use_stream,\n",
    "        max_tokens=4096,\n",
    "        temperature=0.7,\n",
    "        presence_penalty=1.2,\n",
    "        top_p=0.8,\n",
    "    )\n",
    "\n",
    "if use_stream:\n",
    "    answer = \"\"\n",
    "    for chunk in response:\n",
    "        answer += chunk.choices[0].delta.content\n",
    "        display.clear_output(wait=True)\n",
    "        print(answer)\n",
    "else:\n",
    "    print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url=\"http://localhost:8020/asr/funasr/audio_to_text\"\n",
    "\n",
    "headers = {\n",
    "    'Connection': 'keep-alive',\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'\n",
    "}\n",
    "files = {\n",
    "    \"audio\": (\"test.wav\", open(f\"statics/examples/asr_example.wav\", 'rb'))\n",
    "}\n",
    "response = requests.post(url=url, headers=headers, files=files)\n",
    "\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ipex-llm-npu",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
